package com.cakebackoffice;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.cakebackoffice.Model.Address;
import com.cakebackoffice.Model.ProductType;
import com.cakebackoffice.Model.UnitarySale;
import com.cakebackoffice.clientsoap.CakeBackOfficeSoapClient;
import com.cakebackoffice.clientsoap.Exception.SoapClientException;
import com.cakebackoffice.clientsoap.Exception.SoapComException;
import com.cakebackoffice.clientsoap.contract.SimpleClient;
import com.cakebackoffice.clientsoap.contract.SimpleOrder;

/**
 * Created by IntelliJ IDEA.
 * User: Marjorie
 * Date: 19/05/12
 * Time: 17:20
 * To change this template use File | Settings | File Templates.
 */
public class UltimateTestActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ultimatetest);

        ImageView splashScreen = (ImageView) findViewById(R.id.splash_image);

        splashScreen.setOnClickListener(monListenner);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.

    }


    public View.OnClickListener monListenner = new View.OnClickListener() {
        public void onClick(View v) {

            Toast.makeText(getBaseContext(), "Debut", Toast.LENGTH_SHORT).show();
            com.cakebackoffice.Model.Order test = new com.cakebackoffice.Model.Order(new com.cakebackoffice.Model.Client("Jean", "Jacque", "0494577339", new Address("36 rue de la galinette", "Hyères")), null, null, null);
            com.cakebackoffice.Model.ProductType productType = new ProductType("Pouet");
            productType.setRemoteID(1l);
            test.getUnitarySaleList().add(new UnitarySale(42f, 19.6f, productType, null));
            new TestTask().execute(test);
        }
    };

    private class TestTask extends AsyncTask<com.cakebackoffice.Model.Order, Integer, String> {


        @Override
        protected String doInBackground(com.cakebackoffice.Model.Order... objects) {
            CakeBackOfficeSoapClient serveur = new CakeBackOfficeSoapClient();
            try {
                Boolean result = true;
                com.cakebackoffice.Model.Order object = objects[0];
                object.getClient().setRemoteID(Long.parseLong(serveur.createClient(new SimpleClient(objects[0].getClient()))));
                object.setRemoteID(Long.valueOf(serveur.insertOrder(new SimpleOrder(object))));
                for (com.cakebackoffice.Model.UnitarySale unitarySale : object.getUnitarySaleList()) {
                    try {
                        if (unitarySale.isProduct()) {
                            Long id;
                            if ((id = unitarySale.getProductType().getRemoteID()) != null) {
                                if (!serveur.addProductTypeToOrder(id.intValue(), object.getRemoteID().intValue()))
                                    result = false;
                            }
                        }
                    } catch (Exception e) {
                        return e.getMessage();
                    }
                }
                for (com.cakebackoffice.Model.UnitarySale unitarySale : object.getUnitarySaleList()) {
                    try {
                        if (unitarySale.isProduct()) {
                            Long id;
                            if ((id = unitarySale.getProductType().getRemoteID()) != null) {
                                if (!serveur.removeProductTypeFromOrder(id.intValue(), object.getRemoteID().intValue()))
                                    result = false;
                            }
                        }
                    } catch (Exception e) {
                        return e.getMessage();
                    }
                }
                if (!serveur.deleteOrder(object.getRemoteID().intValue())) {
                    result = false;
                }
                return result.toString() + "  " + object.getRemoteID().toString();
            } catch (SoapComException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                return e.getMessage();
            } catch (SoapClientException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                return e.getMessage();
            }

        }

        @Override
        protected void onPostExecute(String s) {
            ((TextView) findViewById(R.id.result)).setText(s);
        }
    }

}